package com.example.wj.controller;

import com.example.wj.api.BaseController;
import com.example.wj.api.TableDataInfo;
import com.example.wj.dto.BookDto;
import com.example.wj.entity.Book;
import com.example.wj.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/books")
public class BookController extends BaseController {

    @Autowired
    private BookService bookService;

    @PostMapping("/add")
    public ResponseEntity<Book> add(@RequestBody BookDto bookDto) {
        return ResponseEntity.ok(this.bookService.insertOrUpdate(bookDto));
    }

    @PostMapping("/edit")
    public ResponseEntity<Book> edit(@RequestBody BookDto bookDto) {
        return ResponseEntity.ok(this.bookService.insertOrUpdate(bookDto));
    }

    @PostMapping("/delete")
    public ResponseEntity<Boolean> deleteById(@RequestBody BookDto bookDto) {
        return ResponseEntity.ok(this.bookService.deleteById(bookDto.getId()));
    }

    @GetMapping("/queryById")
    public ResponseEntity<BookDto> queryById(@RequestParam Integer id) {
        return ResponseEntity.ok(this.bookService.queryById(id));
    }

    @GetMapping("/list")
    public TableDataInfo queryList(@RequestParam String keyWord) {
        startPage();
        return getDataTable(bookService.queryList(keyWord));
    }
}

